﻿namespace Dissemination.MVC.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Web.Mvc;
    using Dissemination.MVC.Repositories;
    using Dissemination.MVC.ViewModels;
    using Dissemination.MVC.Models;

    public class SurveyController : ControllerBase
    {
        protected SurveyRepository repository = new SurveyRepository();

        #region Details

        //
        // GET: /Survey/Details/5

        /// <summary>
        /// Returns the view associated to the details of an entity in the data source.
        /// </summary>
        /// <param name="id">The entity id.</param>
        /// <returns>Details view</returns>
        [Authorize(Roles = "Demostrator,Administrator")]
        public ActionResult Details(int id)
        {
            throw new NotImplementedException();

            if (log.IsDebugEnabled) log.DebugFormat("Entering Survey details with id={0}...", id);
            var survey = repository.Get(id);
            
            if (survey == null)
            {
                if (log.IsErrorEnabled) log.ErrorFormat("Survey with id={0} not found", id);
                return View("NotFound");
            }

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Survey details with id={0}...", id);
            return View(survey);
        }

        #endregion

        #region New

        //
        // GET: /Survey/Create

        /// <summary>
        /// Creates the specified entity.
        /// </summary>
        /// <param name="SurveyID"></param>
        /// <returns>Create view</returns>
        [Authorize(Roles = "Administrator")]
        public ActionResult Create(int programID)
        {
            if (log.IsDebugEnabled) log.Debug("Entering Survey creation (GET)..."); 
            int today = DateTime.Now.Year;
    
            var viewModel = new SurveyViewModel
                                {
                                    Survey = new Survey { Program = this.repository.GetProgramByID(programID) },
                                    Years = new List<int> { today - 1, today, today + 1, today + 2, today + 3 }
                                };

            if (log.IsDebugEnabled) log.Debug("Leaving Survey creation (GET)...");
            return View(viewModel);
        } 

        //
        // POST: /Survey/Create

        /// <summary>
        /// Creates the specified entity.
        /// </summary>
        /// <param name="survey">The entity</param>
        /// <returns>Details view</returns>
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Create(Survey survey)
        {
            if (log.IsDebugEnabled) log.Debug("Entering Survey creation (POST)...");
            int surveyProgramID = int.Parse(RouteData.Values["programID"].ToString());
            survey.Program = repository.GetProgramByID(surveyProgramID);

            if (ModelState.IsValid)
            {
                try
                {
                    repository.Add(survey);

                    foreach (var targetArea in survey.Program.TargetAreas)
                    {
                        repository.AddEmptySurveyResult(survey, targetArea);
                    }

                    repository.Save();

                    if (log.IsDebugEnabled) log.Debug("Leaving Survey creation (POST)...");
                    return RedirectToAction("Details", "Program", new { id = surveyProgramID });
                }
                catch
                {
                }
            }

            int today = DateTime.Now.Year;

            var viewModel = new SurveyViewModel
            {
                Survey = survey,
                Years = new List<int> { today - 1, today, today + 1 }
            };

            if (log.IsDebugEnabled)
            {
                //TODO: completar Create_Survey
            }

            return View(viewModel);

        }

        #endregion

        #region Edit

        //
        // GET: /Survey/Edit/5

        /// <summary>
        /// Returns the view associated with the edit of an existing entity.
        /// </summary>
        /// <param name="id">The id of the entity to edit</param>
        /// <returns>Edit view</returns>
        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Entering Survey with id {0} edit (GET)...", id);
            var survey = repository.Get(id);
            
            if (survey == null)
            {
                if (log.IsErrorEnabled) log.ErrorFormat("Survey with id={0} not found", id);
                return View("NotFound");
            }

            int today = DateTime.Now.Year;

            var viewModel = new SurveyViewModel
            {
                Survey = survey,
                Years = new List<int> { today - 1, today, today + 1, today + 2, today + 3 }
            };

            if (log.IsDebugEnabled) log.DebugFormat("Leaving Survey with id {0} edit (GET)...", id);
            return View(viewModel);
        }

        //
        // POST: /Survey/Edit/5

        /// <summary>
        /// Edits the specified entity.
        /// </summary>
        /// <param name="id">The id of the existing entity.</param>
        /// <param name="collection">The collection of values entered.</param>
        /// <returns>Details view</returns>
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (log.IsDebugEnabled) log.DebugFormat("Entering Survey with id {0} edit (POST)...", id);
            var survey = repository.Get(id);
            try
            {
                UpdateModel(survey, "Survey");

                repository.Save();

                if (log.IsDebugEnabled) log.DebugFormat("Leaving Survey with id {0} edit (POST)...", id);
                return RedirectToAction("Details", "Program", new { id = survey.Program.ProgramID });
            }
            catch (Exception ex)
            {
                int today = DateTime.Now.Year;

                var viewModel = new SurveyViewModel
                {
                    Survey = survey,
                    Years = new List<int> { today - 1, today, today + 1 }
                };

                if (log.IsErrorEnabled) log.Error("Error editing Survey", ex);
                return View(viewModel);
            }
        }

        #endregion

    }
}
